<?php
class Ddup_Featured_Adminhtml_ProductsController extends Mage_Adminhtml_Controller_Action
{
	public function indexAction()
	{
		$this->loadLayout();
		$this->renderLayout();
	}
	
	/**
	 * Add products group
	 * 
	 */
	public function newAction()
	{
		$this->_forward('edit');
	}
	
	/**
	 * Edit products group
	 * 
	 */
	public function editAction()
	{
		$this->loadLayout();
		$this->_addContent($this->getLayout()->createBlock('featured/adminhtml_products_edit'))
            ->_addLeft($this->getLayout()->createBlock('featured/adminhtml_products_edit_tabs'));
		$this->renderLayout();
	}
	
	/**
	 * Save products group
	 * 
	 */
	public function saveAction()
	{
		$post = $this->getRequest()->getPost();
        if ( $post ) {
        	$productsGroup = Mage::getModel('featured/products');
           	$productsGroup->loadPost($post);
           	$productsGroup->save();
        }
        $this->_redirect('*/*/');
	}
	
	/**
	 * Delete Products Group
	 * 
	 */
	public function deleteAction()
	{
		if ($id = $this->getRequest()->getParam('id')) {
            try {
                $productGroup = Mage::getModel('featured/products')
                				->load($id)->delete();
                Mage::getSingleton('adminhtml/session')->addSuccess(
                    Mage::helper('featured')->__('The record has been deleted.')
                );
                $this->_redirect('*/*/');
            } catch (Mage_Core_Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            } catch (Exception $e) {
                $this->_getSession()->addError(
                    Mage::helper('featured')->__('An error occurred while deleting the record. Please review the log and try again.')
                );
                Mage::logException($e);
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            }
        }else {
	        Mage::getSingleton('adminhtml/session')->addError(
	            Mage::helper('featured')->__('Unable to find a record to delete.')
	        );
	        $this->_redirect('*/*/');
        }
	}
	
	public function productGridAction()
	{
		if( $this->getRequest()->getParam('id') ) {
            $productsGroupData = Mage::getModel('featured/products')
                ->load($this->getRequest()->getParam('id'));
            Mage::register('products_group_data', $productsGroupData);
        }
		$this->getResponse()->setBody(
    			$this->getLayout()->createBlock('featured/adminhtml_products_edit_tab_grid')->toHtml()
    	);
	}
	
}